/*
 *  language.c
 *
 *  $Id$	
 *
 */

#include <stdio.h>
#include <string.h>

#include "language.h"
#include "log.h"

static int current_language = LANG_EN;

static const char* language_getstringinternal(int languageid, int stringid);

// British English
static struct stringtable strings_en[] = {
	{ STRING_PLAYER1NAME,		"Player1 Name:" },
	{ STRING_PLAYER2NAME,		"Player2 Name:" },
	{ STRING_GAMEBEGIN,			"Thank you. Now the game can begin.\n" },
	{ STRING_PLAYER1DETAIL,		"\tPlayer 1 (%s): X\n" },
	{ STRING_PLAYER2DETAIL,		"\tPlayer 2 (%s): 0\n" },
	{ STRING_GOFIRST,			"\n\t%s will go first!.\n" },
	{ STRING_ITADRAW,			"\nIt's a draw!!\n" },
	{ STRING_YOUAREWINNER,		"\nCongratulations %s, you are the winner!!\n" },
	{ STRING_PLAYAGAIN,			"Play again? [y/n]:" },
	{ STRING_MOVEPROMPT,		"Player %d (%s) (x,y): " },
	{ STRING_INVALIDMOVE,		"\nThat's not a valid selection. Please try again.\n" },
	{ STRING_BADMOVEYOU,		"Sorry, but you have already moved there.\n" },
	{ STRING_BADMOVEOTHER,		"Sorry, but %s has already moved there.\n" },
	{ 0, 0 }
};

// Norwegian Bokmål
static struct stringtable strings_no[] = {
	{ STRING_PLAYER1NAME,		"Spiller1 Navn:" },
	{ STRING_PLAYER2NAME,		"Spiller2 Navn:" },
	{ STRING_GAMEBEGIN,			"Takk. Nå kan spillet begynne.\n" },
	{ STRING_PLAYER1DETAIL,		"\tSpiller 1: (%s): X\n" },
	{ STRING_PLAYER2DETAIL,		"\tSpiller 2: (%s): 0\n" },
	{ STRING_GOFIRST,			"\n\t%s vil begynne!.\n" },
	{ STRING_ITADRAW,			"\nDet står uavgjort!!\n" },
	{ STRING_YOUAREWINNER,		"\nGratulerer %s, du er vinneren!!\n" },
	{ STRING_PLAYAGAIN,			"Spille igjen? [y/n]:" },
	{ STRING_MOVEPROMPT,		"Spiller %d (%s) (x,y): " },
	{ STRING_INVALIDMOVE,		"\nDet er ikke en gyldig markering. Prøv igjen.\n" },
	{ STRING_BADMOVEYOU,		"Beklager, men du har allerede flyttet dit.\n" },
	{ STRING_BADMOVEOTHER,		"Beklager, men %s er allerede flyttet dit.\n" },
	{ 0, 0}
};

static struct languagetable languages[] = {
	{ LANG_EN, "en", "English (British)", strings_en },
	{ LANG_NO, "no", "Norwegian (Bokmål)", strings_no },
	{ LANG_US, "us", "English (American)", strings_en }, // american gets british strings for now
	{ 0, 0, 0}
};

const struct languagetable* language_findbyid(int languageid) {
	struct languagetable* lang = languages;
	do {
		if (lang->id == languageid)
			return lang;
	} while (++lang != NULL);
	return NULL;
} //

const struct languagetable* language_findbycode(const char* code) {
	struct languagetable* lang = languages;
	do {
		if (strcmp(lang->code, code) == 0)
			return lang;
	} while ((++lang)->strings != NULL);
	return NULL;
} //

static const char* language_getstringinternal(int languageid, int stringid) {
	const struct languagetable* lang = language_findbyid(languageid);
	if (lang) {
		struct stringtable* st = lang->strings;
		do {
			if (st->stringid == stringid)
				return st->text;
		} while((++st)->text != NULL);
	}
	return NULL;
} //

void language_setcurrent(int languageid) {
	current_language = languageid;
} //

const char* language_getstring(int stringid) {
	return language_getstringinternal(current_language, stringid);
} //

int language_findidbycode(const char* code) {
	const struct languagetable* lang = language_findbycode(code);
	if (lang)
		return lang->id;
	return 0; // not found
} //
